package org.sixtynine.stock.dao.impl;

/**
 * @author Thilanka
 *
 */

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.dao.UserCategoryDao;
import org.sixtynine.stock.entity.UserCategory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class UserCategoryDaoImpl extends AbstractDaoImpl<UserCategory, Integer>
		implements UserCategoryDao {

	protected UserCategoryDaoImpl() {
		super(UserCategory.class);
	}

	@Override
	public void saveUserCategory(UserCategory userCategory) {
		saveOrUpdate(userCategory);
	}

	@Override
	public UserCategory findUserCategory(String name) {
		List<UserCategory> userCatList = findByCriteria(Restrictions.eq("name",
				name));
		if (userCatList.size() > 0) {
			return userCatList.get(0);
		}
		return null;
	}

	@Override
	@Transactional
	public void deleteModules(int userCategoryId) {
		String sql = "delete from CategoryModule where userCategory = :userCateId";

		Query query = getCurrentSession().createQuery(sql);
		query.setInteger("userCateId", userCategoryId);

		query.executeUpdate();

	}

}
